//
//  UITableView+Extension.swift
//  Library_y
//
//  Created by JC on 2023/6/6.
//

import Foundation
import UIKit

public extension UITableViewCell {
    
    // 点击效果图片
    func setCustomSelectedBV() {
        let v = UIView(frame: frame)
        v.backgroundColor = UIColor(white: 0, alpha: 0.05)
        selectedBackgroundView = v
    }
}

public extension UITableView {
    
    /// 刷新某一行
    func reloadIndexPath(_ indexPath: IndexPath) {
        reloadRows(at: [indexPath], with: .none)
    }
    
    /// 刷新某一行
    func reloadSectionRow(_ section: Int, _ row: Int) {
        let indexPath = IndexPath(row: row, section: section)
        reloadRows(at: [indexPath], with: .none)
    }
}

public extension UITableView {
    
    /// 注册带xib的cell
    ///
    /// - Parameters:
    ///   - type: cell类名
    ///   - identifier: 复用id
    func registerNibName<T: UITableViewCell>(type: T.Type, identifier: String = "\(T.self)") {
        register(UINib.init(nibName: "\(type.self)", bundle: nil), forCellReuseIdentifier: identifier)
    }
    
    func registerReusableCell<T: UITableViewCell>(withIdentifier: String, type: T.Type) {
        register(type, forCellReuseIdentifier: withIdentifier)
    }
    
    func registerReusableCell<T: UITableViewCell>(with type: T.Type) {
        registerReusableCell(withIdentifier: "\(type.self)", type: type)
    }
    
    func dequeueReusableCell<T: UITableViewCell>(withIdentifier: String, type: T.Type, style: UITableViewCell.CellStyle = .default) -> T {
        return (dequeueReusableCell(withIdentifier: withIdentifier) as? T) ?? T(style: style, reuseIdentifier: withIdentifier)
    }
    
    func dequeueReusableCell<T: UITableViewCell>(with type: T.Type, style: UITableViewCell.CellStyle = .default) -> T {
        return dequeueReusableCell(withIdentifier: "\(type.self)", type: type, style: style)
    }
}

extension UITableView {
    
    /// 设置tableView头部空格
    public func setHeaderSpace() {
        let view = UIView()
        view.frame = CGRect(x: 0, y: 0, width: Device_y.screenWidth, height: 20)
        view.backgroundColor = .clear
        tableHeaderView = view
    }
    
    /// 设置tableView头部空格
    public func setFooterSpace(_ backColor: UIColor = .black) {
        let view = UIView()
        view.frame = CGRect(x: 0, y: 0, width: Device_y.screenWidth, height: 30)
        view.backgroundColor = .clear
        tableFooterView = view
    }
    
    /// 设置tableView头部尾部空格
    public func setHeaderFooterSpace() {
        setHeaderSpace()
        setFooterSpace()
    }

    /// 设置tableView头部空格
    public func setFooterNil() {
        let view = UIView()
        view.frame = CGRect(x: 0, y: 0, width: Device_y.screenWidth, height: 1)
        view.backgroundColor = .white
        tableFooterView = view
    }
}
